package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.sky.vo.DishVO;
import jdk.net.SocketFlow;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.sky.constant.MessageConstant.DISH_BE_RELATED_BY_SETMEAL;
import static com.sky.constant.MessageConstant.DISH_ON_SALE;


/**
 * @author iTaotao
 * @date 2023/7/28 13:03
 * @desciption: 菜品接口实现类
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageResult selectPage(Long categoryId, String name, Integer page, Integer pageSize, Integer status) {
        PageHelper.startPage(page, pageSize);
        Dish dish = new Dish();
        dish.setName(name);
        dish.setCategoryId(categoryId);
        dish.setStatus(status);
        Page<Dish> dishPage = dishMapper.selectPage(dish);
        long total = dishPage.getTotal();
        List<Dish> result = dishPage.getResult();
        return new PageResult(total, result);


    }

    @Override
    public void createDish(DishDTO dishDTO) {
        Set keys = redisTemplate.keys("dishes::*");
        if(keys.size() > 0){
            redisTemplate.delete(keys);
        }
            
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.insertDish(dish);

        extracted(dishDTO, dish);


    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeDishById(List<Long> ids) {
        Set keys = redisTemplate.keys("dishes::*");
        if(keys.size() > 0){
            redisTemplate.delete(keys);
        }
//        存在起售就不能删除
        Integer countNum = dishMapper.selectByIdWithStatus(ids , StatusConstant.ENABLE);
        if(countNum > 0){
            throw new DeletionNotAllowedException(DISH_ON_SALE);
        }
//        被套餐关联的菜品不能删除
        Integer countNumber = setmealDishMapper.selectByDishId(ids);
        if(countNumber > 0){
            throw new DeletionNotAllowedException(DISH_BE_RELATED_BY_SETMEAL);
        }

        dishMapper.deleteDishById(ids);
        dishFlavorMapper.deleteFlavorByDishId(ids);

    }

    @Override
    public DishVO queryDishById(Long id) {
        Dish dish = dishMapper.selectDishById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectFlavorByDishId(id);
        if (dishFlavorList != null && dishFlavorList.size() > 0) {
            dishVO.setFlavors(dishFlavorList);
        }
        return dishVO;

    }

    @Override
    public void updateDishById(DishDTO dishDTO) {
        Set keys = redisTemplate.keys("dishes::*");
        if(keys.size() > 0){
            redisTemplate.delete(keys);
        }
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO , dish);
        dishMapper.updateDishById(dish);

        ArrayList<Long> dishIds = new ArrayList<>();
        dishIds.add(dishDTO.getId());
        dishFlavorMapper.deleteFlavorByDishId(dishIds);

        extracted(dishDTO , dish);
    }

    @Override
    public void updateStatusById(Integer status, Long id) {
        Set keys = redisTemplate.keys("dishes::*");
        if(keys.size() > 0){
            redisTemplate.delete(keys);
        }
        dishMapper.updateStatusById(status , id);
    }

    @Override
    public List<Dish> selectList(Long categoryId) {
        List<Dish> dishList = dishMapper.selectList(categoryId);
        return dishList;

    }


    private void extracted(DishDTO dishDTO, Dish dish) {
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> flavor.setDishId(dish.getId()));
            dishFlavorMapper.insertBranch(flavors);
        }
    }
}
